<?php
namespace Admin\Controller;
use Com\Wechat;
use Com\WechatAuth;
class WechatMenuController extends CommonController
{

    /**
     * 列表
     *
     * @param void
     * @return void
     */
    public function index()
    {
        if (IS_POST) {
            $this->delete();
        } else {
            $where['status'] = 1;
            $list = M('WechatMenu')->where($where)->order('sort DESC, menu_id ASC')->select();
            $list = list_to_tree($list, 'menu_id', 'parent_id', 'child');
            $this->assign('list', $list);
            $this->display();
        }
    }
    
    /**
     * 新增
     *
     * @param  void
     * @return void
     */
    public function add()
    {
        if (IS_POST) {
            $data = I('post.');
            $menu = D('WechatMenu');
            $result = $menu->update($data);
            if (false !== $result) {
                $this->success('保存成功', U('index'));
            } else {
                $error = $menu->getError();
                $this->error(empty($error) ? '未知错误' : $error);
            }
        } else {
            $parentID = I('get.parent_id', 0);
            $cate = M('WechatMenu')->where(array( 'parent_id' => 0, 'status' => 1))->order('sort DESC')->getField('menu_id,menu_title');
            $this->assign('parent_id', $parentID);
            $this->assign('cate', $cate);
            $this->display();
        }
    }

    /**
     * 修改
     *
     * @param void
     * @return void
     */
    public function edit()
    {
        if (IS_POST) {
            $data = I('post.');
            $menu = D('WechatMenu');
            $result = $menu->update($data);
            if (false !== $result) {
                $this->success('保存成功', U('index'));
            } else {
                $error = $menu->getError();
                $this->error(empty($error) ? '未知错误' : $error);
            }
        } else {
            $id = I('get.id');
            if (empty($id)) {
                $this->error('无效的数据');
            }
            $info = M('WechatMenu')->where('menu_id = ' . $id)->find();
            $cate = M('WechatMenu')->where(array( 'parent_id' => 0, 'status' => 1))->order('sort DESC')->getField('menu_id,menu_title');
            $this->assign('cate', $cate);
            $this->assign('info', $info);
            $this->display();
        }
    }

    /**
     * 删除
     *
     * @param void
     * @return void
     */
    public function delete()
    {
        $id = array_unique((array) I('id', 0));
        if (empty($id)) {
            $this->error('请选择要操作的数据!');
        }
        $where = array( 'menu_id' => array( 'in', $id ) );
        $data = array( 'status' => 0, 'update_time' => time() );
        $result = M('WechatMenu')->where($where)->save($data);
        if ($result) {
            $this->success('删除成功');
        } else {
            $this->error('删除失败');
        }
    }  
    
    /**
     * ajax
     *
     * @param void
     * @return void
     */
    public function ajax()
    {
        $get = I('get.');
        if (! isset($get['branch']) || ! isset($get['id']) || ! isset($get['column']) || ! isset($get['value'])) {
            exit('false');
        }
        if ($get['branch'] == 'menu_sort'){
            $data = array( $get['column'] => $get['value'], 'update_time' => time() );
            $result = M('WechatMenu')->where(array('menu_id' => $get['id'] ))->save($data);
            exit($result ? 'true' : 'false');
            
        } else {
            exit('false');
        }

    }

    public function menu_create()
    {
        $token = getAccessToken();
        $auth = new WechatAuth(C('WECHAT_APPID'), C('WECHAT_APPSECRET'), $token);

        $list = M('WechatMenu')->where(array('status' => 1))->order('sort DESC, menu_id ASC')->select();
        $list = list_to_tree($list, 'menu_id', 'parent_id', 'child');
        $menu = array();
        foreach ($list as $val) {
            $sub = array();
            if ($val['child']) {
                foreach ($val['child'] as $value) {
                    $tmp = array();
                    $tmp['type'] = getMenuType($value['menu_type']);
                    $tmp['name'] = $value['menu_title'];
                    $tmp['key'] = $value['menu_name'];
                    if (!empty($value['menu_url'])) {
                        $tmp['url'] = $value['menu_url'];
                    }
                    $sub[] = $tmp;
                }
            }
            if (empty($sub)) {
                $menu[] = array('type' => getMenuType($val['menu_type']), 'name' => $val['menu_title'], 'key' => $val['menu_name'], 'url' => $val['menu_url']);
            } else {
                $menu[] = array('name' => $val['menu_title'], 'key' => $val['menu_name'], 'sub_button' => $sub);
            }
        }
        dump($menu);
        $result = $auth->menuCreate($menu);
//        $result = $auth->menuDelete();

        dump($result);
    }

    public function menu_delete()
    {
        $token = getAccessToken();
        $auth = new WechatAuth(C('WECHAT_APPID'), C('WECHAT_APPSECRET'), $token);
        $result = $auth->menuDelete();
        dump($result);
    }
    
}